<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${packageName}.mapper.${entityName}Mapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        INSERT INTO ${tableName} (
        <#list columns as column>
            <#if column_index != 0>, </#if> ${column.column_name}
        </#list>
        )
        VALUES (
        <#list columns as column>
            <#if column_index != 0>, </#if> <#noparse>#{</#noparse>${column.camel_case_column_name}<#noparse>}</#noparse>
        </#list>
        )
    </insert>
    <update id="update">
        UPDATE ${tableName}
        <set>
            <#list columns as column>
                ${r'<if test="'}${column.camel_case_column_name}!= null and ${column.camel_case_column_name} <#noparse> !=''"> </#noparse><#if column_index != 0>, </#if>${column.column_name} = <#noparse>#{</#noparse>${column.camel_case_column_name}<#noparse>}</#noparse>${r'</if>'}
            </#list>
        </set>
        WHERE id = <#noparse>#{id} </#noparse>
    </update>
    <delete id="delete">
        DELETE FROM ${tableName}
        WHERE id = <#noparse>#{id} </#noparse>
    </delete>

    <select id="getModel" resultType="${packageName}.entity.vo.${entityName}Vo">
        SELECT *
        FROM ${tableName}
        WHERE id = <#noparse> ${param1} </#noparse> LIMIT 1
    </select>

    <update id="updateDelFlagById">
        UPDATE ${tableName}
        SET del_flag =<#noparse> ${param2} </#noparse>
        WHERE id = <#noparse> ${param1} </#noparse>
    </update>
    <select id="countPage" resultType="java.lang.Integer">
        SELECT COUNT(1)
        FROM ${tableName}
        <include refid="${lowerEntityName}_where"/>
    </select>
    <select id="listPage" resultType="${packageName}.entity.vo.${entityName}Vo">
        SELECT *
        FROM ${tableName}
        <include refid="${lowerEntityName}_where"/>
        <if test="size!= null">
            LIMIT
            <if test="startIndex!= null">
                <#noparse>  ${startIndex}, </#noparse>
                ${r'</if>'}
                <#noparse>${size}</#noparse>
            </if>
    </select>

    <sql id="${lowerEntityName}_where">
        <where>
            1 = 1
            <#list columns as column>
                ${r'<if test="entity.'}${column.camel_case_column_name}${r'!= null and entity.'}${column.camel_case_column_name} <#noparse>!=''"></#noparse>  AND ${column.column_name} = <#noparse>#{entity.</#noparse>${column.camel_case_column_name}<#noparse>}</#noparse>${r'</if>'}
            </#list>
        </where>
    </sql>
</mapper>
